#include "ConstSmallFile.h"
#include "ConstSmallFileProxy.h"
#include "wrap/fcntl.h"
#include "wrap/unistd.h"


ConstSmallFile::ConstSmallFile(const char *filename):
    _proxy{}, _fd{Wrap::open_(filename, Wrap::o_rdonly() | Wrap::o_cloexec())}
{
    if (_fd > 0) {
        _proxy = std::make_unique<BufferConstSmallFileProxy>(_fd);
    }
    else {
        _proxy = std::make_unique<ConstSmallFileProxy>();
    }
}

ConstSmallFile::~ConstSmallFile() {
    if (_fd < 0) {
        return;
    }
    Wrap::close_(_fd);
}

std::uintmax_t ConstSmallFile::file_size() const {
    return _proxy->file_size();
}

ssize_t ConstSmallFile::read() {
    return _proxy->read();
}

std::string ConstSmallFile::read_no_more_than(std::size_t maxSize) {
    return _proxy->read_no_more_than(maxSize);
}
